﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Globalization;

namespace KgUtils.Wpf.ValidationRules
{
    /// <summary>
    /// Validates if the binding contains a value
    /// </summary>
    public class RequiredRule : ValidationRule
    {
        public string FieldDisplayName { get; set; }

        /// <summary>
        /// Validates if the value is not null or empty in case it is a string
        /// </summary>
        /// <param name="value">Value to validate</param>
        /// <param name="cultureInfo">Culture information required for parsing</param>
        /// <returns>Returns the result of the validation operation</returns>
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            ValidationResult result = new ValidationResult(true, null);

            if (value == null || (value is string && String.IsNullOrEmpty(value.ToString())))
            {
                result = new ValidationResult(false, String.Format("Le champ {0} est obligatoire", FieldDisplayName));
            }

            return result;
        }
    }
}
